<template>
  <div class="content">
    <div class="category">
      <div
        v-for="item in categories"
        :key="item.name"
        :class="{'category__item': true, 'category__item--active': currentTab === item.tab}"
        @click="() => handleTabClick(item.tab)"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="product">
      <div
        class="product__item"
        v-for="item in list"
        :key="item._id"
      >
        <img class="product__item__img" :src="item.imgUrl" />
        <div class="product__item__detail">
          <h4 class="product__item__title">{{item.name}}</h4>
          <p class="product__item__sales">月售{{item.sales}}件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;</span>{{item.price}}
            <span class="product__item__origin">&yen;{{item.oldPrice}}</span>
          </p>
        </div>
        <div class="product__number">
          <span
            class="product__number__minus iconfont icon-jianhao"
            @click="() => { changeCartItem(shopId, item._id, item, -1,shopName) }">
          </span>
            {{getProductCartCount(shopId, item._id)}}
          <span
            class="product__number__plus iconfont icon-jiahao-tianchong"
            @click="() => { changeCartItem(shopId, item._id, item, 1,shopName) }">
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref, toRefs, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { get } from '../../utils/request'
import {useCommonCartEffect} from '../../effects/cartEffects'
import { useStore } from 'vuex'

const categories = [
  { name: '全部商品', tab: 'all' },
  { name:'秒杀', tab: 'seckill' },
  { name: '新鲜水果', tab: 'fruit'}
];

// Tab 切换相关的逻辑
const useTabEffect = () => {
  const currentTab = ref(categories[0].tab)
  const handleTabClick = (tab) => {
    currentTab.value = tab
  }
  return { currentTab, handleTabClick }
}

// 列表内容相关的逻辑
const useCurrentListEffect = (currentTab,shopId) => {
  const content = reactive({ list: [] })
  const getContentData = async () => {
    const result = await get(`/api/shop/${shopId}/products?tab=${currentTab.value}`)
    if(result?.errno === 0 && result?.data?.length) {
      // console.log(result.data);
      content.list = result.data;
    }
  }
  watchEffect(() => { getContentData('all') })
  const { list } = toRefs(content)
  return { list }
}

//购物车相关逻辑
const useCartContentEffect = ()=>{
  const store = useStore();
  const { changeCartItemInfo,cartList} = useCommonCartEffect() 
  const changeShopName = (shopId,shopName)=>{
    store.commit('changeShopName',{shopId,shopName});
  }
  const changeCartItem = (shopId,productId,item,num,shopName)=>{
    changeCartItemInfo(shopId,productId,item,num);
    changeShopName(shopId,shopName);
  }
  const getProductCartCount = (shopId,productId)=>{
    return cartList?.[shopId]?.productList?.[productId]?.count || 0;
  }
  return {cartList,changeCartItem,getProductCartCount}
}

export default {
  name: "Content",
  props:['shopName'],
  setup(){
      const route = useRoute();
      const shopId = route.params.id;
      const { currentTab, handleTabClick } = useTabEffect();
      const { list } = useCurrentListEffect(currentTab,shopId);
      const {cartList,changeCartItem,getProductCartCount}=useCartContentEffect();
      
      return {
        categories, currentTab, handleTabClick, list,shopId,changeCartItem,cartList,getProductCartCount
    }
  }
};
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  position: absolute;
  left: 0;
  right: 0;
  top: 1.5rem;
  bottom: 0.5rem;
}
.category {
  overflow-y: scroll;
  height: 100%;
  width: 0.76rem;
  background: #f5f5f5;
  &__item {
    line-height: 0.4rem;
    text-align: center;
    font-size: 0.14rem;
    color: #333;
    &--active {
      background: #FFFFFF;
    }
  }
}
.product {
  overflow-y: scroll;
  flex: 1;
  &__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: .01rem solid #f1f1f1;
    &__detail {
      overflow: hidden;
    }
    &__img {
      width: 0.68rem;
      height: 0.68rem;
      margin-right: 0.16rem;
    }
    &__title {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #333333;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &__sales {
      margin: 0.06rem 0;
      font-size: 0.12rem;
      color: #333333;
    }
    &__price {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #E93B3B;
    }
    &__yen {
      font-size: 0.12rem;
    }
    &__origin {
      margin-left: 0.06rem;
      line-height: 0.2rem;
      font-size: 0.12rem;
      color: #999999;
      text-decoration: line-through;
    }
    .product__number {
      position: absolute;
      right: 0;
      bottom: 0.16rem;
      line-height: 0.18rem;
      &__minus {
        position: relative;
        top: 0.02rem;
        color: #666;
        margin-right: 0.07rem;
      }
      &__plus {
        position: relative;
        top: 0.02rem;
        color: #0091FF;
        margin-left: 0.04rem;
      }
    }
  }
}
</style>
